package com.jing.weibo.scheduler.utils;

/**
 * CronExpConversion
 * @author jing.yue
 * @version 2012/09/18 1.0.0
 */
public class CronExpConversion {

	/**
	 * 页面设置转为UNIX cron expressions 转换算法
	 *  @param  everyWhat
	 *  @param  commonNeeds 包括 second minute hour
	 *  @param  monthlyNeeds 包括 第几个星期 星期几
	 *  @param  weeklyNeeds  包括 星期几
	 *  @param  userDefinedNeeds  包括具体时间点
	 *  @return  cron expression
	 */
	public   static  String convertDateToCronExp(String everyWhat,
			String[] commonNeeds, String[] monthlyNeeds, String weeklyNeeds,
			String userDefinedNeeds)  {
		String cronEx  =   "" ;
		String commons  =  commonNeeds[0] + " " + commonNeeds[1] + " " + commonNeeds[2] + " ";
		String dayOfWeek  =   "" ;
		if  ( "monthly" .equals(everyWhat))  {
			//  eg.: 6#3 (day 6 = Friday and "#3" = the 3rd one in the
			//  month)
			dayOfWeek  =  monthlyNeeds[ 1 ]
			                            +  CronExRelated.specialCharacters
			                            .get(CronExRelated._THENTH)  +  monthlyNeeds[ 0 ];
			cronEx  =  (commons
					+  CronExRelated.specialCharacters.get(CronExRelated._ANY)
					+   "   "
					+  CronExRelated.specialCharacters.get(CronExRelated._EVERY)
					+   "   "   +  dayOfWeek  +   "   " ).trim();
		} else if ( "weekly" .equals(everyWhat))  {
			dayOfWeek  =  weeklyNeeds;  //  1
			cronEx  =  (commons
					+  CronExRelated.specialCharacters.get(CronExRelated._ANY)
					+   "   "
					+  CronExRelated.specialCharacters.get(CronExRelated._EVERY)
					+   "   "   +  dayOfWeek  +   "   " ).trim();
		} else if("hourly".equals(everyWhat)){ //每多少小时

			cronEx = commonNeeds[0] + " "   +  commonNeeds[1] + " ";

			if("1".equals(commonNeeds[2])) cronEx += CronExRelated.specialCharacters.get(CronExRelated._EVERY) + " ";
			else cronEx += commonNeeds[2] + " ";

			cronEx 	+= CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._ANY);

		} else if("minutely".equals(everyWhat)){ //每多少分钟
			cronEx = commonNeeds[0] + " ";
			if("1".equals(commonNeeds[1])) cronEx += CronExRelated.specialCharacters.get(CronExRelated._EVERY) + " ";
			else cronEx += commonNeeds[1] + " ";

			cronEx 	+= CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._ANY);

		} else if("secondly".equals(everyWhat)){ //每多少秒
			cronEx = "";

			if("1".equals(commonNeeds[0])) cronEx += CronExRelated.specialCharacters.get(CronExRelated._EVERY) + " ";
			else cronEx += commonNeeds[0] + " ";

			cronEx 	+= CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._EVERY)
			+ " "
			+ CronExRelated.specialCharacters.get(CronExRelated._ANY);

		} else if  ( "userDefined" .equals(everyWhat))  {
			String dayOfMonth  =  userDefinedNeeds.split( "-" )[ 2 ];
			if  (dayOfMonth.startsWith( "0" ))  {
				dayOfMonth  =  dayOfMonth.replaceFirst( "0" ,  "" );
			}
			String month  =  userDefinedNeeds.split( "-" )[ 1 ];
			if  (month.startsWith( "0" ))  {
				month  =  month.replaceFirst( "0" , "" );
			}
			// FIXME 暂时不加年份 Quartz报错
			//	           String year  =  userDefinedNeeds.split( " - " )[ 0 ];
			/* cronEx = (commons + dayOfMonth + " " + month + " "
	                   + CronExRelated.specialCharacters.get(CronExRelated._ANY)
	                   + " " + year).trim(); */
			cronEx  =  (commons  +  dayOfMonth  +   " "   +  month  +   " "
					+  CronExRelated.specialCharacters.get(CronExRelated._ANY)
					+   " " ).trim();
		}
		return cronEx;
	}


	public static void main(String[] args) {

		//		String cron = CronExpConversion.convertDateToCronExp("userDefined", new String[]{"0","0","12"}, null, null, "2012-06-29");
		String cron = CronExpConversion.convertDateToCronExp("secondly", new String[]{"0","0","1"}, null, null, null);
		//		String cron = CronExpConversion.convertDateToCronExp("userDefined",);
		System.out.println(cron);
	}
}
